package com.qzq.weibo.fragment;

import com.qzq.weibo.R;
import com.qzq.weibo.entity.UnreadCount;
import com.qzq.weibo.http.HttpRequest;
import com.qzq.weibo.utils.WeiCozzz;
import com.sina.weibo.sdk.constant.WBConstants.Msg;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MessageFragment extends Fragment {

	private static MessageFragment mInstance;

	private View rootView;

	/** 显示的item */
	private LinearLayout ll_at;
	private LinearLayout ll_comment;
	private LinearLayout ll_good;
	private LinearLayout ll_notice;

	/** 显示的消息内容 */
	private TextView tv_atText;
	private TextView tv_commentText;
	private TextView tv_goodText;
	private TextView tv_noticeText;

	/** 显示的消息时间 */
	private TextView tv_atTime;
	private TextView tv_commentTime;
	private TextView tv_goodTime;
	private TextView tv_noticeTime;

	/** 显示的消息气泡，默认是隐藏的 */
	private RelativeLayout rl_atBubble;
	private RelativeLayout rl_commentBubble;
	private RelativeLayout rl_goodBubble;
	private RelativeLayout rl_noticeBubble;

	/** 消息气泡中显示的消息数目 */
	private TextView tv_atBubbleNum;
	private TextView tv_commentBubbleNum;
	private TextView tv_goodBubbleNum;
	private TextView tv_noticeBubbleNum;

	/** 服务器中获取未读消息数据 */
	private UnreadCount unreadCount;

	/** 判断是否有新的消息 */
	private boolean hasNewCmt;
	private boolean hasNewAt;
	private boolean hasNewGood;
	private boolean hasNewNotice;

	private HttpRequest http;

	public static MessageFragment getInstance() {
		if (mInstance == null) {
			mInstance = new MessageFragment();
		}

		return mInstance;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (rootView != null) {
			ViewGroup parent = (ViewGroup) rootView.getParent();
			if (parent != null) {
				parent.removeView(rootView);
			}

			return rootView;
		} else {

			View view = initView(inflater);
			initMember();
			loadingUnreadCount();

			rootView = view;
			return view;
		}
	}

	/** 初始化页面布局 */
	private View initView(LayoutInflater inflater) {
		View view = inflater.inflate(R.layout.fragment_message, null);

		/** 初始化item控件 */
		ll_at = (LinearLayout) view.findViewById(R.id.message_ll_at);
		ll_comment = (LinearLayout) view.findViewById(R.id.message_ll_comment);
		ll_good = (LinearLayout) view.findViewById(R.id.message_ll_good);
		ll_notice = (LinearLayout) view.findViewById(R.id.message_ll_notice);

		/** 初始化消息内容控件 */
		tv_atText = (TextView) view.findViewById(R.id.message_at_tv_text);
		tv_commentText = (TextView) view
				.findViewById(R.id.message_comment_tv_text);
		tv_goodText = (TextView) view.findViewById(R.id.message_good_tv_text);
		tv_noticeText = (TextView) view
				.findViewById(R.id.message_notice_tv_text);

		/** 初始化消息时间控件 */
		tv_atTime = (TextView) view.findViewById(R.id.message_at_tv_time);
		tv_commentTime = (TextView) view
				.findViewById(R.id.message_comment_tv_time);
		tv_goodTime = (TextView) view.findViewById(R.id.message_good_tv_time);
		tv_noticeTime = (TextView) view
				.findViewById(R.id.message_notice_tv_time);

		/** 初始化消息气泡控件 */
		rl_atBubble = (RelativeLayout) view
				.findViewById(R.id.message_at_rl_bubble);
		rl_commentBubble = (RelativeLayout) view
				.findViewById(R.id.message_comment_rl_bubble);
		rl_goodBubble = (RelativeLayout) view
				.findViewById(R.id.message_good_rl_bubble);
		rl_noticeBubble = (RelativeLayout) view
				.findViewById(R.id.message_notice_rl_bubble);

		/** 初始化消息气泡中的数字控件 */
		tv_atBubbleNum = (TextView) view
				.findViewById(R.id.message_at_bubble_tv_num);
		tv_commentBubbleNum = (TextView) view
				.findViewById(R.id.message_comment_bubble_tv_num);
		tv_goodBubbleNum = (TextView) view
				.findViewById(R.id.message_good_bubble_tv_num);
		tv_noticeBubbleNum = (TextView) view
				.findViewById(R.id.message_notice_bubble_tv_num);

		return view;
	}

	/** 初始化成员变量 */
	private void initMember() {

		unreadCount = new UnreadCount();
		http = HttpRequest.getInstance(getActivity());

	}

	@SuppressLint("HandlerLeak")
	private void loadingUnreadCount() {
		http.requestGetUnreadCount(new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if (msg != null) {
					int flag = msg.what;
					if (flag == HttpRequest.REQUEST_FAILER) {
						HttpRequest.badRequest(
								Integer.parseInt(msg.obj.toString()),
								getActivity());
					} else {
						unreadCount = (UnreadCount) msg.obj;
						WeiCozzz.getInstance().setUnreadCount(unreadCount);
						updateUi();
					}
				}
			}
		});
	}

	private void updateUi() {
		/** 有新的评论时 */
		if (unreadCount.getCmt() > 0) {
			hasNewCmt = true;
			rl_commentBubble.setVisibility(View.VISIBLE);
			tv_commentBubbleNum.setText(String.valueOf(unreadCount.getCmt()));
		} else {
			hasNewCmt = false;
			rl_commentBubble.setVisibility(View.GONE);
		}

		/** 有新的@时 */
		if (unreadCount.getMention_cmt() > 0
				|| unreadCount.getMention_status() > 0) {
			hasNewAt = true;
			rl_atBubble.setVisibility(View.VISIBLE);
			tv_atBubbleNum.setText(String.valueOf(unreadCount.getMention_cmt()
					+ unreadCount.getMention_status()));
		} else {
			hasNewAt = false;
			rl_atBubble.setVisibility(View.GONE);
		}

		/** 有新的赞时 ,API暂未支持 */

		/** 有新的通知时 */
		if (unreadCount.getNotice() > 0) {
			hasNewNotice = true;
			rl_noticeBubble.setVisibility(View.VISIBLE);
			tv_noticeBubbleNum.setText(String.valueOf(unreadCount.getNotice()));
		} else {
			hasNewNotice = false;
			rl_noticeBubble.setVisibility(View.GONE);
		}
	}
}
